package com.puboot.module.blog.controller;

import com.puboot.common.util.*;
import com.puboot.module.admin.vo.base.ResponseVo;
import com.puboot.module.blog.model.*;
import com.puboot.module.blog.service.*;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/blog/ceramics/api")
@AllArgsConstructor
public class CeramicsController {

    private final HttpServletRequest httpServletRequest;
    private final CeramicsProcessService ceramicsProcessService;
    private final CeramicsService ceramicsService;
    private final CeramicsDetailService ceramicsDetailService;

    @PostMapping("/result")
    @Transactional
    public ResponseVo result(Ceramics ceramics) {
        try {
            HttpSession httpSession = httpServletRequest.getSession();
            Customer customer = (Customer) httpSession.getAttribute("customer");

            List<String> fs = new ArrayList<>();
            if (StringUtils.isNotBlank(ceramics.getF())) {
                fs = Arrays.asList(ceramics.getF().split(","));
            }
            List<String> others = new ArrayList<>();
            if (StringUtils.isNotBlank(ceramics.getOther())) {
                others = Arrays.asList(ceramics.getOther().split(","));
            }
            List<String> ccaco3s = new ArrayList<>();
            if (StringUtils.isNotBlank(ceramics.getCcaco3())) {
                ccaco3s = Arrays.asList(ceramics.getCcaco3().split(","));
            }
            List<String> cmgco3s = new ArrayList<>();
            if (StringUtils.isNotBlank(ceramics.getCmgco3())) {
                cmgco3s = Arrays.asList(ceramics.getCmgco3().split(","));
            }
            List<String> ncvs = new ArrayList<>();
            if (StringUtils.isNotBlank(ceramics.getNcv())) {
                ncvs = Arrays.asList(ceramics.getNcv().split(","));
            }
            List<String> fcs = new ArrayList<>();
            if (StringUtils.isNotBlank(ceramics.getFc())) {
                fcs = Arrays.asList(ceramics.getFc().split(","));
            }
            List<String> ccs = new ArrayList<>();
            if (StringUtils.isNotBlank(ceramics.getCc())) {
                ccs = Arrays.asList(ceramics.getCc().split(","));
            }
            List<String> ofs = new ArrayList<>();
            if (StringUtils.isNotBlank(ceramics.getOf())) {
                ofs = Arrays.asList(ceramics.getOf().split(","));
            }
            Double eBurn = SpinUtil.getEBurn(ncvs, fcs, ccs, ofs);
            Double eProcess = CeramicsUtil.getEProcess(fs, others, ccaco3s, cmgco3s);
            Double ePurchaseElectric = SpinUtil.getEPurchaseElectric(ceramics.getAdPurchaseElectric(), ceramics.getEfPurchaseElectric());
            Double ePurchaseHeat = SpinUtil.getEPurchaseHeatOrEOutputHeat(ceramics.getAdPurchaseHeat());
            Double eOutputHeat = SpinUtil.getEPurchaseHeatOrEOutputHeat(ceramics.getAdOutInputHeat());
            Double eOutputElectric = SpinUtil.getEOutputElectric(ceramics.getAdOutInputElectric(), ceramics.getEfOutInputElectric());
            Double result = CeramicsUtil.getE(eBurn, eProcess, ePurchaseElectric,
                    ePurchaseHeat, eOutputElectric, eOutputHeat);
            ceramics.setCustomerId(customer.getId());
            ceramics.setCreateTime(new Date());
            ceramics.setEBurn(GasDCUtil.round(eBurn, 2));
            ceramics.setEProcess(GasDCUtil.round(eProcess, 2));
            ceramics.setEPurchaseElectric(GasDCUtil.round(ePurchaseElectric, 2));
            ceramics.setEPurchaseHeat(GasDCUtil.round(ePurchaseHeat, 2));
            ceramics.setEOutputHeat(GasDCUtil.round(eOutputHeat, 2));
            ceramics.setEOutputElectric(GasDCUtil.round(eOutputElectric, 2));
            ceramics.setState(Constants.STATE_INVAILD);
            ceramics.setResult(GasDCUtil.getThousandth(result));
            ceramicsService.save(ceramics);
            for (int i = 0; i < fs.size(); i++) {
                CeramicsProcess ceramicsProcess = new CeramicsProcess();
                ceramicsProcess.setF(Double.valueOf(fs.get(i)));
                ceramicsProcess.setOther(Double.valueOf(others.get(i)));
                ceramicsProcess.setCcaco3(Double.valueOf(ccaco3s.get(i)));
                ceramicsProcess.setCmgco3(Double.valueOf(cmgco3s.get(i)));
                ceramicsProcess.setCeramicsId(ceramics.getId());
                ceramicsProcessService.save(ceramicsProcess);
            }
            for (int i = 0; i < ncvs.size(); i++) {
                CeramicsDetail cementDetail = new CeramicsDetail();
                cementDetail.setNcv(Double.valueOf(ncvs.get(i)));
                cementDetail.setFc(Double.valueOf(fcs.get(i)));
                cementDetail.setCc(Double.valueOf(ccs.get(i)));
                cementDetail.setOf(Double.valueOf(ofs.get(i)));
                cementDetail.setCeramicsId(ceramics.getId());
                ceramicsDetailService.save(cementDetail);
            }
            return ResultUtil.success("计算成功", ceramics);
        } catch (Exception e) {
            log.error("计算失败", e);
            return ResultUtil.error("计算失败");
        }
    }


}
